/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.table;

import com.jspx.boot.environment.Environment;
import com.jspx.boot.sign.MappingType;
import com.jspx.json.JSONObject;
import com.jspx.sober.annotation.*;
import com.jspx.txweb.IRole;
import com.jspx.txweb.IUserSession;
import com.jspx.utils.StringUtil;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2010-7-27
 * Time: 15:22:59
 *     
 */
@Table(name="jspx_user_session",caption="在线信息",cache=true,cacheName= Environment.userSessionCache)
public class UserSession implements IUserSession  {
    @Id(type= IDType.serial)
    @Column(caption="ID",notNull = true)
    private long id;

   //@Id(auto = false)避免key冲突
    @Column(caption="sid",length = 50,dataType="isLengthBetween(5,50)", notNull = true)
    private String sid;

    @Column(caption="隐藏",option="0:在线;1:隐身;2:忙碌;3:请勿打扰", defaultValue = "0", notNull = true)
    private int invisible = 0;

    @Column(caption="用户ID",notNull = true, defaultValue = "0")
    private long uid = 0;

    @Nexus(mapping = MappingType.OneToOne, name = "uid", targetName = "id", targetEntity = Member.class,delete = false,chain=true,where="${uid!=0}")
    private Member member = new Member();

    @Column(caption = "名称", dataType = "isLengthBetween(1,250)", length = 250)
    private String url = StringUtil.empty;

    @Column(caption = "浏览器", dataType = "isLengthBetween(1,100)", length = 100, notNull = true)
    private String browser = "unknown";

    @Column(caption = "操作系统", dataType = "isLengthBetween(1,100)", length = 100, notNull = true)
    private String system = "unknown";

    @Column(caption = "网络类型", dataType = "isLengthBetween(1,20)", length = 20)
    private String netType = "unknown";

    @Column(caption="IP地址",length = 50, notNull = true)
    public String ip = "127.0.0.1";

    @Column(caption="最后请求时间",notNull = true)
    private long lastRequestTime = System.currentTimeMillis();

    @Column(caption="登陆时间",notNull = true)
    private Date createDate = new Date();

    public UserSession()
    {

    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getSid()
    {
        return sid;
    }

    public void setSid(String sid)
    {
        this.sid = sid;
    }

    public long getUid() {
        return uid;
    }

    public void setUid(long uid) {
        this.uid = uid;
    }

    public String getName() {
        if (member==null)
        {
            member = new Member();
        }
        return StringUtil.isNULL(member.getName())?"游客":member.getName();
    }

    public String getNickname()
    {
        if (member==null)
        {
            member = new Member();
        }
        return StringUtil.isNULL(member.getNickname())?StringUtil.empty:member.getNickname();
    }

    public int getInvisible() {
        return invisible;
    }

    public void setInvisible(int invisible) {
        this.invisible = invisible;
    }

    public Member getMember() {
        if (member==null)
        {
            member = new Member();
        }
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
        if (member != null)
        {
            this.uid = member.getId();
        }
        else
        {
            this.uid = 0;
        }
    }

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    public String getBrowser()
    {
        return browser;
    }

    public void setBrowser(String browser)
    {
        this.browser = browser;
    }

    public String getSystem()
    {
        return system;
    }

    public void setSystem(String system)
    {
        this.system = system;
    }

    public String getNetType()
    {
        return netType;
    }

    public void setNetType(String netType)
    {
        this.netType = netType;
    }

    /**
     * 每个软件用户所既有的角色不同,使用拦截器拦截后放入
    */
    public IRole getRole(String softName)
    {
        if (member==null) return new Role();
        return member.getRole(softName);
    }

    public void setRole(IRole role)
    {
        if (member==null) member = new Member();
        member.setRole(role);
    }

    public String getIp()
    {
        return ip;
    }

    public void setIp(String ip)
    {
        this.ip = ip;
    }

    public Date getCreateDate()
    {
        return createDate;
    }

    public void setCreateDate(Date createDate)
    {
        this.createDate = createDate;
    }

    public long getLastRequestTime()
    {
        return lastRequestTime;
    }

    public void setLastRequestTime(long lastRequestTime)
    {
        this.lastRequestTime = lastRequestTime;
    }

    public boolean isGuest() {
        return uid <= 0 || member == null || member.getId() == 0;
    }
    @Override
    public String toString()
    {
        JSONObject json = new JSONObject();
        json.put("id",id);
        json.put("sid",sid);
        json.put("uid",uid);
        json.put("name",getName());
        json.put("browser",browser);
        json.put("system",system);
        json.put("netType",netType);
        json.put("ip",ip);
        json.put("createDate",createDate);
        return json.toString(4);
    }
}